<?php
/**
 * form控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Form extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('form_model');

		$this->loadModel('response_model');

		$this->loadClass('Mcache');

		$this->tpl->template_dir = SYS_PATH  . 'template/web/form/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{
		echo "hello world";
	}

	/**
	 * 获取预览页面
	 * @return unknown_type
	 */
	function getPreview(){

		$tpl_name = trim($this->in_var['modal'])?trim($this->in_var['modal']):"preview_common";
		$tpl_type = trim($this->in_var['type']);

		switch($tpl_type){
			case 1:
				$this->getPreviewWLimit($tpl_name.".html");
				break;
			default:
				$this->getPreviewDefault($tpl_name.".html");
				break;
		}
	}
	/**
	 * 获取预览
	 * @return max
	 */
	private function getPreviewDefault($tpl_name){

		$key = $this->in_var['key'];

		if($this->tpl->isCached($tpl_name, $key))
		{
			$this->tpl->displayCache($tpl_name, $key);
			exit;
		}

		$form = $this->form_model->getFormByKey($key);

		$this->result['form'] = $form;

		$this->tpl->assign("result",$this->result);

		$this->tpl->displayCache($tpl_name,$key);
	}

	/**
	 * 验证cookie的预览
	 * @return max
	 */
	private function getPreviewWLimit($tpl_name){

		$key = $this->in_var['key'];

		$has_submit = intval($_COOKIE['has_submit']);

		$form = $this->form_model->getFormByKey($key);

		if($has_submit)
		{
			/*if($this->tpl->isCached($tpl_name, $key))
			{
				$this->tpl->displayCache($tpl_name, $key);
				exit;
			}*/

			$this->tpl->assign("has_submit",'ok');
			$this->tpl->assign("prompt_msg",$this->sys_conf_var['survey']['response']['submit_success']);

		}
		
		$this->result['form'] = $form;

		$cache = $this->Mcache->getCache("survey",$key);
		
		$this->tpl->assign("result",$this->result);
		$this->tpl->assign("percent",$cache['Entries']);

		$this->tpl->display($tpl_name,$key);

	}

	/**
	 * 表单提交
	 * @return response
	 */
	function responseForm(){

		$response = array();
		$key_	  = $this->in_var['key'];
		$ip		  = $this->in_var['IP_ADDRESS'];
		$time	  = time();
		$method	  = $this->in_var["request_method"];

		if($method == "post")
		{

			$seccode  = trim(strtolower($this->in_var['seccode']));
			$sec      = trim(strtolower($_SESSION['login_check_number']));

			if(empty($seccode)||$seccode != $sec)
			{
				exit($this->sys_conf_var['survey']['response']['submit_error']);
			}
			else
			{
				unset($_SESSION['login_check_number']);
			}

			try{
				$formdata = array();

				foreach($this->in_var as $key=>$val)
				{
					if(is_numeric($key))
					{
						if(is_array($val))
						{
							$val = json_encode($val);
						}

						$arr['r_entry_id'] 	  = $key;
						$arr['r_answer'] 	  = $val;
						$arr['r_submit_time'] = $time;
						$arr['r_submit_ip']   = $ip;
						$arr['r_form_key']    = $key_;

						$formdata[$arr['r_entry_id']] = $arr;
						$this->response_model->addResponseItem($arr);
					}
				}

				$this->addCompleteHit($key_);

				$old_data = $this->Mcache->getCache("survey",$key_);

				if(!empty($old_data))
				{
					$new_analizy = $this->response_model->resetAnalizyed($key_,$formdata,$old_data);
					$this->Mcache->setCache("survey",$key_,$new_analizy);
				}

				//用户会话配置
				//setCookie("has_submit",1,time()+$subtime);

				exit($this->sys_conf_var['survey']['response']['submit_success']);

			}catch(Exception $e)
			{
				exit($this->sys_conf_var['survey']['response']['submit_error']);
			}

		}
		else
		{
			exit($this->sys_conf_var['survey']['response']['submit_error']);
		}

	}

	/**
	 * cookie记录方式提交
	 * @return unknown_type
	 */
	function responseFormWlimit(){
		$response = array();
		$key_	  = $this->in_var['key'];
		$ip		  = $this->in_var['IP_ADDRESS'];
		$time	  = time();
		$method	  = $this->in_var["request_method"];

		if($method == "post")
		{

			try{
				$formdata = array();

				foreach($this->in_var as $key=>$val)
				{
					if(is_numeric($key))
					{
						if(is_array($val))
						{
							$val = json_encode($val);
						}

						$arr['r_entry_id'] 	  = $key;
						$arr['r_answer'] 	  = $val;
						$arr['r_submit_time'] = $time;
						$arr['r_submit_ip']   = $ip;
						$arr['r_form_key']    = $key_;

						$formdata[$arr['r_entry_id']] = $arr;
						$this->response_model->addResponseItem($arr);
					}
				}

				$this->addCompleteHit($key_);

				$old_data = $this->Mcache->getCache("survey",$key_);
				
				if(!empty($old_data))
				{
					$new_analizy = $this->response_model->resetAnalizyed($key_,$formdata,$old_data);
					$this->Mcache->setCache("survey",$key_,$new_analizy);
				}else{
					$new_analizy = $this->response_model->analizyResponse($key_);
					$this->Mcache->setCache("survey",$key_,$new_analizy);
				}
				
				//用户会话配置
				setCookie("has_submit",1,time()+3600*24);
				
				$data = $this->Mcache->getCache("survey",$key_);
				
				exit(json_encode(array('info'=>$this->sys_conf_var['survey']['response']['submit_success'],'status'=>true,'entries'=>$data['Entries'])));

			}catch(Exception $e)
			{
				exit(json_encode(array('info'=>$this->sys_conf_var['survey']['response']['submit_error'],'status'=>false)));
			}

		}
		else
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['survey']['response']['submit_error'],'status'=>false)));
		}
	}

	/**
	 * 表单完成量处理
	 * @return
	 */
	function addCompleteHit($formKey){

		$submitNum = intval($this->Mcache->getCache("survey","submitNum"));

		$this->Mcache->setCache("survey","submitNum",++$submitNum);

		$submitNum = intval($this->Mcache->getCache("survey","submitNum"));

		if($submitNum > $this->sys_conf_var['survey']['hit']['maxcache'])
		{
			$this->form_model->addCompleteHit($formKey,$submitNum);

			$this->Mcache->clearCache("survey","submitNum");
		}
	}

	/**
	 * 点击量处理
	 * @return unknown_type
	 */
	function addHitCount($key){

		$submitNum = intval($this->Mcache->getCache("survey","hitNum"));

		$this->Mcache->setCache("survey","hitNum",++$submitNum);

		$hitNum = intval($this->Mcache->getCache("survey","hitNum"));

		if($hitNum > $this->sys_conf_var['survey']['hit']['maxcache'])
		{
			$this->form_model->addHitCount($key,$hitNum);

			$this->Mcache->clearCache("survey","hitNum");
		}
	}

	/**
	 * 判断当前广告位是否有广告，如果有则返回对应js无则返回空
	 * @return unknown_type
	 */
	function proxy(){

		$zone_id = intval($this->in_var['ads']);
		$b_id    = intval($this->in_var['banner']);

		$banner = $this->Mcache->getCache('banner',$b_id);

		if(empty($banner))
		{
			$banner = $this->ads_banner_model->getBannerByZone($zone_id);
			$this->Mcache->setCache('banner',$b_id,$banner);
		}

		if(empty($banner['b_status'])) exit;
		header("Content-Type: text/javascript");
		exit(file_get_contents("http://res.61.com/js/ads/info.js"));
	}
}